from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import QWidget, QHBoxLayout, QListWidget, QFrame, QVBoxLayout, QLabel, QListWidgetItem, \
    QPushButton

from common.equipment.reference import Equipment
from common.player.reference import Player
from common.resources import Resource
from gui.reference import SIZE_WIDTH, SIZE_HEIGHT
from gui.signals import SIGNALS
from gui.widgets_map import open_window
from utils.db import DBSession


class EquipRow(QWidget):
    def __init__(self, equip: Equipment):
        super().__init__()

        label = QLabel(equip.overview(show_durability=False))
        label.setFixedHeight(30)
        label.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        btn = QPushButton('穿上')
        btn.setFixedHeight(30)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(label)

        layout.addWidget(label, 9)
        layout.addWidget(btn, 1)
        self.setLayout(layout)


class MaterialRow(QWidget):
    def __init__(self, material, num):
        super().__init__()

        label = QLabel(material)
        label.setFixedHeight(30)
        label.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        num_label = QLabel(str(num))
        num_label.setFixedHeight(30)
        num_label.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        layout.addWidget(label, 9)
        layout.addWidget(num_label, 1)
        self.setLayout(layout)


class __Frame(QFrame):
    def __init__(self, title, data):
        super().__init__()

        self.setFrameShape(QFrame.Shape.Box)
        self.setLineWidth(1)

        self.data = data

        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        label = QLabel(f'<h1>{title}</h1>')
        label.setFixedHeight(30)
        layout.addWidget(label)

        self.list_widget = QListWidget()
        self.list_widget.setStyleSheet("QListWidget::item{height: 30px}")
        self.list_widget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
        layout.addWidget(self.list_widget)

        self.update_data()

    def update_data(self):
        self.list_widget.clear()
        for data in self.data:
            item = QListWidgetItem()
            item.setData(Qt.ItemDataRole.UserRole, data)
            self.list_widget.addItem(item)
            self.list_widget.setItemWidget(item, self.create_row_widget(data))

    def create_row_widget(self, data):
        return QLabel(data)


class EquipFrame(__Frame):
    signal_install_equip = Signal(Equipment)

    def create_row_widget(self, data: Equipment):
        w = QWidget()
        label = QLabel(data.overview(show_durability=False))
        label.setFixedHeight(30)
        label.setAlignment(Qt.AlignmentFlag.AlignVCenter)

        btn = QPushButton('穿上')
        btn.setFixedHeight(30)

        btn.clicked.connect(lambda: self.signal_install_equip.emit(data))

        btn_detail = QPushButton('详细')
        btn_detail.setFixedHeight(30)
        btn_detail.clicked.connect(lambda: open_window('equip_detail', data))

        layout = QHBoxLayout(w)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(label)

        layout.addWidget(label, 9)
        layout.addWidget(btn_detail, 1)
        layout.addWidget(btn, 1)

        return w


class ConsumableFrame(__Frame):
    def create_row_widget(self, key):
        w = QWidget()
        return w


class MaterialFrame(__Frame):
    def create_row_widget(self, key):
        num = self.data[key]
        r: Resource = DBSession.materials[key]

        w = QWidget()
        label = QLabel(r.name)
        label.setFixedHeight(30)

        num_label = QLabel(str(num))
        num_label.setFixedHeight(30)
        layout = QHBoxLayout(w)
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        layout.addWidget(label, 9)
        layout.addWidget(num_label, 1)

        return w


class DialogBag(QWidget):
    def __init__(self, player: Player):
        super().__init__()

        self.setGeometry(200, 200, SIZE_WIDTH, SIZE_HEIGHT)
        self.setWindowTitle(player.bag.title)
        self.player = player

        layout = QHBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.setSpacing(0)

        self.equip_frame = EquipFrame('装备', player.bag.equipment)
        self.equip_frame.signal_install_equip.connect(self.install_equip)

        self.consumable_frame = ConsumableFrame('消耗品', player.bag.consumable)
        self.material_frame = MaterialFrame('材料', player.bag.material)

        layout.addWidget(self.equip_frame)
        layout.addWidget(self.consumable_frame)
        layout.addWidget(self.material_frame)

        self.setLayout(layout)

        SIGNALS.update_bag.connect(self.update_data)

    def update_data(self):
        self.equip_frame.update_data()
        self.consumable_frame.update_data()
        self.material_frame.update_data()
        self.setWindowTitle(self.player.bag.title)

    def install_equip(self, equip: Equipment):
        equip.install(self.player)
        self.equip_frame.update_data()
        self.setWindowTitle(self.player.bag.title)
